package ok;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class HistoryQuizSystem extends JFrame implements ActionListener {

    private JLabel questionLabel;                                     //题目标签
    private JRadioButton optionARadioButton, optionBRadioButton,
            optionCRadioButton, optionDRadioButton;                   //A,B,C,D选项按钮
    private JButton nextButton;                                       //下一题按钮
    public ArrayList<Question> questions;//问题列表
    public int currentQuestionIndex;                                 //当前问题序号
    ButtonGroup optionButtonGroup;
    public static ErrorsFrame errorsFrame;
    public HistoryQuizSystem() {
        errorsFrame=new ErrorsFrame();
        errorsFrame.setVisible(false);
        setTitle("中国现代史纲要刷题系统");                                //title
        setSize(600, 400);                                //设置长宽
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);                                  //窗口相对于屏幕居中显示

        // 初始化组件
        questionLabel = new JLabel();
        optionARadioButton = new JRadioButton("A");
        optionBRadioButton = new JRadioButton("B");
        optionCRadioButton = new JRadioButton("C");
        optionDRadioButton = new JRadioButton("D");

        //按钮群 （单选）
        optionButtonGroup = new ButtonGroup();
        optionButtonGroup.add(optionARadioButton);
        optionButtonGroup.add(optionBRadioButton);
        optionButtonGroup.add(optionCRadioButton);
        optionButtonGroup.add(optionDRadioButton);

        nextButton = new JButton("下一题");
        nextButton.addActionListener(this);

        // 设置布局
        setLayout(new GridLayout(6, 1));
        add(questionLabel);
        add(optionARadioButton);
        add(optionBRadioButton);
        add(optionCRadioButton);
        add(optionDRadioButton);
        add(nextButton);
        // 读取题目数据
        QuestionList questionList = new QuestionList();
        questions = questionList.loadQuestions("C:\\Users\\LENOVO\\Desktop\\questions..txt");
        showQuestion(currentQuestionIndex);

    }



    private void showQuestion(int index) {

        if (index >= 0 && index < questions.size()) {
            Question question = questions.get(index);

            questionLabel.setText(question.getQuestion());
            optionARadioButton.setText(question.getOptionA());
            optionBRadioButton.setText(question.getOptionB());
            optionCRadioButton.setText(question.getOptionC());
            optionDRadioButton.setText(question.getOptionD());


            optionARadioButton.setSelected(false);
            optionBRadioButton.setSelected(false);
            optionCRadioButton.setSelected(false);
            optionDRadioButton.setSelected(false);
        }
    }


    //程序处理用户点击“下一题”按钮的事件。如果用户已经选择了一个答案，程序会检查答案的正确性，并在界面上显示下一题或者弹出恭喜消息框
    @Override
    public void actionPerformed(ActionEvent e) {
        String selectedAnswer = "";
        if (optionARadioButton.isSelected() || optionBRadioButton.isSelected()
                || optionCRadioButton.isSelected() || optionDRadioButton.isSelected()) {
            // 获取用户选择的答案
            if (optionARadioButton.isSelected()) {
                selectedAnswer = "A";
            } else if (optionBRadioButton.isSelected()) {
                selectedAnswer = "B";
            } else if (optionCRadioButton.isSelected()) {
                selectedAnswer = "C";
            } else if (optionDRadioButton.isSelected()) {
                selectedAnswer = "D";
            }

            // 检查答案是否正确
            Question currentQuestion = questions.get(currentQuestionIndex);
            boolean isCorrect = currentQuestion.isCorrectAnswer(selectedAnswer);
            // 显示下一题或结束
            if(!isCorrect){
                errorsFrame.textArea.append(currentQuestion.getQuestion());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("A.");
                errorsFrame.textArea.append(currentQuestion.getOptionA());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("B.");
                errorsFrame.textArea.append(currentQuestion.getOptionB());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("C.");
                errorsFrame.textArea.append(currentQuestion.getOptionC());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("D.");
                errorsFrame.textArea.append(currentQuestion.getOptionD());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("您的选择是：");
                errorsFrame.textArea.append(selectedAnswer);
                errorsFrame.textArea.append("        ");
                errorsFrame.textArea.append("正确答案是：");
                errorsFrame.textArea.append(currentQuestion.getCorrectAnswer());
                errorsFrame.textArea.append("\n");
                errorsFrame.textArea.append("\n");
            }
            optionButtonGroup.setSelected(optionButtonGroup.getSelection(),false);
            if (currentQuestionIndex < questions.size() - 1) {
                currentQuestionIndex++;
                showQuestion(currentQuestionIndex);
            } else {
                JOptionPane.showMessageDialog(this, "恭喜你完成所有题目！");
                errorsFrame.setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(this, "请选择一个答案");
        }
    }
}

